/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paqu;

import java.util.Hashtable;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author Windows XP
 */
public class TextMessage extends TextBox implements CommandListener {

    private MIDlet midlet;
    private Command cmdSend;
    private Command cmdExit;
    private Hashtable items;

    public TextMessage(MIDlet midlet) {
        super("Mensaje a enviar", "", 166, TextField.ANY);
        this.midlet = midlet;
        items = new Hashtable();

        this.createUI();
    }

    public void commandAction(Command c, Displayable d) {
        try {
            if (c == cmdSend) {
                this.sendSMS();
            } else if (c == cmdExit) {
                this.midlet.notifyDestroyed();
            }
        } catch (Exception e) {
            // En caso de error modificamos el texto de la ventana con el mensaje
            this.setString(e.toString());
        }
    }

    private void createUI() {
        this.setTicker(new Ticker("Autentia Real Bussiness Solution"));
        this.cmdExit = new Command("Salir", Command.STOP, 1);
        this.cmdSend = new Command("Enviar", Command.OK, 1);

        this.addCommand(cmdSend);
        this.addCommand(cmdExit);
        this.setCommandListener(this);
    }

    private void sendSMS() {
        String[] array = new String[10];
        String sms = this.getString();
        array[0] = sms.substring(0, 2);
        array[1] = sms.substring(2, 4);
        array[2] = sms.substring(4, 6);
        array[3] = sms.substring(6, 8);
        array[4] = sms.substring(8, 10);
        array[5] = sms.substring(10, 12);
        array[6] = sms.substring(12, 14);
        array[7] = sms.substring(14, 16);
        array[8] = sms.substring(16, 18);
        array[9] = sms.substring(18, 20);

        //Aquí hago el paso al Hashtable
        items.put(array[0], "Sonido 1");
        items.put(array[1], "Sonido 2");
        items.put(array[2], "Sonido 3");
        items.put(array[3], "Sonido 4");
        items.put(array[4], "Sonido 5");
        items.put(array[5], "Sonido 6");
        items.put(array[6], "Sonido 7");
        items.put(array[7], "Sonido 8");
        items.put(array[8], "Sonido 9");
        items.put(array[9], "Sonido 10");

    }
}
